package com.kma.summer2012.client.presenters.authorization;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.kma.summer2012.client.CrumbsEventBus;
import com.kma.summer2012.client.Crypto;
import com.kma.summer2012.client.events.ShowPreloaderEvent;
import com.kma.summer2012.client.events.authorization.LoginEvent;
import com.kma.summer2012.client.presenters.Presenter;
import com.kma.summer2012.client.services.AbstractCallBack;
import com.kma.summer2012.client.services.user.CrumbsUserService;

public class ChangePasswordPresenter implements Presenter {

    public interface Display {
        HasClickHandlers getSaveButton();

        String getPasswordTextBox();

        String getPasswordConfirmTextBox();

        void setPasswordTextBox(String text);

        void setPasswordConfirmTextBox(String text);

        CheckBox getShowPassword();

        void changePasswordTextBoxType();

        void setErrorLabel(String text);

        Widget asWidget();
    }

    private final Display display;

    public ChangePasswordPresenter(Display display) {
        this.display = display;
        bind();
    }

    private void bind() {
        this.display.getShowPassword().addValueChangeHandler(new ValueChangeHandler<Boolean>() {
            @Override
            public void onValueChange(ValueChangeEvent<Boolean> event) {
                ChangePasswordPresenter.this.display.changePasswordTextBoxType();
            }
        });
        this.display.getSaveButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                doCheck();
            }
        });
    }

    private void doCheck() {
        if (this.display.getPasswordTextBox().equals(this.display.getPasswordConfirmTextBox())) {
            if (this.display.getPasswordConfirmTextBox().length() > 5) {
                String[] a = Window.Location.getHref().split("id=");
                CrumbsEventBus.getInstance().fireEvent(new ShowPreloaderEvent());
                CrumbsUserService.App.getInstance().update(Integer.parseInt(Crypto.decrypt(a[1])), ChangePasswordPresenter.this.display.getPasswordTextBox(), new AbstractCallBack() {
                    @Override
                    public void onSuccess(Object o) {
                        Window.alert("Your Password has been successfully changed!");
                        CrumbsEventBus.getInstance().fireEvent(new LoginEvent());
                        ChangePasswordPresenter.this.display.setPasswordConfirmTextBox("");
                        ChangePasswordPresenter.this.display.setPasswordTextBox("");
                    }
                });


                //  CrumbsEventBus.getInstance().fireEvent(new ConfirmPasswordEvent());
            } else {
                this.display.setErrorLabel("Password is too short!");
            }
        } else {
            this.display.setErrorLabel("Passwords don't match!");
        }
    }

    @Override
    public void go(HasWidgets container) {
        container.clear();
        container.add(display.asWidget());
    }
}
